<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="ruler"></div>

<div class="info">
	<a href="https://github.com/yomotsu/camera-controls">GitHub repo</a><br>
	<button onclick="fit()">fit</button>
	<button onclick="cameraControls.reset( true )">reset</button><br>
</div>

<script async src="https://unpkg.com/es-module-shims@1.8.2/dist/es-module-shims.js"></script>
<script type="importmap">
{
	"imports": {
		"three": "https://unpkg.com/three@0.161.0/build/three.module.js",
		"three/addons/": "https://unpkg.com/three@0.161.0/examples/jsm/"
	}
}
</script>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import CameraControls from './dist/camera-controls.module.js';

CameraControls.install( { THREE: THREE } );

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
renderer.physicallyCorrectLights = true;
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild( renderer.domElement );

const cameraControls = new CameraControls( camera, renderer.domElement );

new THREE.TextureLoader().load( './env.jpg', ( equirectangularMap ) => {

	const pmremGenerator = new THREE.PMREMGenerator( renderer );
	pmremGenerator.compileEquirectangularShader();
	const envMapRenderTarget = pmremGenerator.fromEquirectangular( equirectangularMap );
	envMapRenderTarget.texture.encoding = THREE.sRGBEncoding;
	equirectangularMap.dispose();

	scene.environment = envMapRenderTarget.texture;

} );

const gridHelper = new THREE.GridHelper( 50, 50 );
scene.add( gridHelper );

new GLTFLoader().load( './rubber-duck.glb', function ( gltf ) {

	const boundingSphere = CameraControls.createBoundingSphere( gltf.scene );
	const sphereHelper = new THREE.Mesh(
		new THREE.SphereGeometry( boundingSphere.radius, 16, 16 ),
		new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
	);
	sphereHelper.position.copy( boundingSphere.center );

	scene.add( gltf.scene );
	scene.add( sphereHelper );

	window.fit = function() {

		cameraControls.fitToSphere( gltf.scene, true );

	}

	renderer.render( scene, camera );

} );

renderer.render( scene, camera );

( function anim () {

	const delta = clock.getDelta();
	const elapsed = clock.getElapsedTime();
	const updated = cameraControls.update( delta );

	// if ( elapsed > 30 ) { return; }

	requestAnimationFrame( anim );

	if ( updated ) {

		renderer.render( scene, camera );
		console.log( 'rendered' );

	}

} )();

// make variable available to browser console
globalThis.cameraControls = cameraControls;
</script>

</body>
</html>
